﻿using System;
using System.Linq;
using System.Windows.Forms;
using HighDiveEngine.Aggregates;
using HighDiveEngine.Model;

namespace HighDiveAdmin.GUI.Components
{
    /// <summary>
    /// Control for handling the connectionphase of a contest
    /// </summary>
    public partial class RunningContestConnectionsGroupBox : UserControl
    {
        /// <summary>
        /// Triggered when the Start Contest button is clicked
        /// </summary>
        public event MethodInvoker EventStartContest;
        /// <summary>
        /// Triggered when the Start Offline button is clicked
        /// </summary>
        public event MethodInvoker EventStartOffline;
        /// <summary>
        /// Triggered when the Cancel button is clicked
        /// </summary>
        public event MethodInvoker EventCancel;

        public RunningContestConnectionsGroupBox()
        {
            InitializeComponent();
        }

        #region ButtonClickEvents
        private void buttonStartContest_Click_1(object sender, EventArgs e)
        {
            if (EventStartContest != null)
                EventStartContest();
        }

        private void buttonStartOffline_Click(object sender, EventArgs e)
        {
            if (EventStartOffline != null)
                EventStartOffline();
        }

        private void buttonCancelStartContest_Click(object sender, EventArgs e)
        {
            if (EventCancel != null)
                EventCancel();
        } 
        #endregion

        #region FillFunctions
        /// <summary>
        /// Updates all the controls associated with the judge to reflect hishers connection status
        /// </summary>
        /// <param name="judge">The judge whose status has been updated</param>
        public void UpdateJudgeConnectionStatus(Judge judge)
        {
            switch (judge.JudgeNumber)
            {
                case 1: _updateJudgeStatusLabels(labelConnectionJudge1, labelConnectionStatusJudge1, judge); break;
                case 2: _updateJudgeStatusLabels(labelConnectionJudge2, labelConnectionStatusJudge2, judge); break;
                case 3: _updateJudgeStatusLabels(labelConnectionJudge3, labelConnectionStatusJudge3, judge); break;
                case 4: _updateJudgeStatusLabels(labelConnectionJudge4, labelConnectionStatusJudge4, judge); break;
                case 5: _updateJudgeStatusLabels(labelConnectionJudge5, labelConnectionStatusJudge5, judge); break;
                case 6: _updateJudgeStatusLabels(labelConnectionJudge6, labelConnectionStatusJudge6, judge); break;
                case 7: _updateJudgeStatusLabels(labelConnectionJudge7, labelConnectionStatusJudge7, judge); break;
            }
        }

        private void _updateJudgeStatusLabels(Label judgeLabel, Label statusLabel, Judge judge)
        {
            lock (judge.Status)
            {
                statusLabel.ForeColor = judgeLabel.ForeColor = judge.Status.Color;
                statusLabel.Text = judge.Status.Text;
                judgeLabel.Text = judge.IdString;
            }
            toolTipInfo.SetToolTip(judgeLabel, judge.FirstName + " " + judge.LastName);
        }

        public void FillAdminIp(string ipAddress)
        {
            labelIp.Text = ipAddress;
        }

        /// <summary>
        /// Sets the amount of controls to show according to how many judges are assigned in the contest
        /// </summary>
        /// <param name="judgeList">List of all the judges in the contest</param>
        public void SetNumVisibleJudges(JudgeList judgeList)
        {
            foreach (Label lbl in groupBoxJudgeConnection.Controls.OfType<Label>())
            {
                lbl.Visible = false;
            }
            labelAdminIp.Visible = true;
            labelIp.Visible = true;

            switch (judgeList.NumberOfJudges)
            {
                case 7: labelConnectionJudge7.Visible = labelConnectionStatusJudge7.Visible = true; goto case 6;
                case 6: labelConnectionJudge6.Visible = labelConnectionStatusJudge6.Visible = true; goto case 5;
                case 5: labelConnectionJudge5.Visible = labelConnectionStatusJudge5.Visible = true; goto case 4;
                case 4: labelConnectionJudge4.Visible = labelConnectionStatusJudge4.Visible = true; goto case 3;
                case 3: labelConnectionJudge3.Visible = labelConnectionStatusJudge3.Visible = true; goto case 2;
                case 2: labelConnectionJudge2.Visible = labelConnectionStatusJudge2.Visible = true; goto case 1;
                case 1: labelConnectionJudge1.Visible = labelConnectionStatusJudge1.Visible = true; break;
            }

            foreach (var judge in judgeList)
            {
                UpdateJudgeConnectionStatus(judge);
            }
        }
        #endregion

        #region TabStop
        private void RunningContestConnectionsGroupBox_TabStopChanged(object sender, EventArgs e)
        {
            foreach (Control control in Controls)
                control.TabStop = TabStop;
        } 
        private void RunningContestConnectionsGroupBox_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
                buttonStartContest.Focus();
        }
        #endregion

    }
}
